import java.util.PriorityQueue;

class Solution {
    public int minRefuelStops(int target, int startFuel, int[][] stations) {
        PriorityQueue<Integer> heap = new PriorityQueue<>((a, b) -> b - a); // 大顶堆，用于存储加油站的油量
        int ret = 0, pre = 0, currentFuel = startFuel;

        for (int[] station : stations) {
            int stationPosition = station[0];
            int stationFuel = station[1];

            while (currentFuel < stationPosition - pre) {
                if (heap.isEmpty()) {
                    return -1;
                }
                currentFuel += heap.poll();
                ret++;
            }

            currentFuel -= stationPosition - pre;
            heap.offer(stationFuel);
            pre = stationPosition;
        }

        while (currentFuel < target - pre) {
            if (heap.isEmpty()) {
                return -1;
            }
            currentFuel += heap.poll();
            ret++;
        }

        return ret;
    }

    public static void main(String[] args) {
        Solution solution = new Solution();
        int[][] stations = new int[4][2];
        stations[0] = new int[]{10, 60};
        stations[1] = new int[]{20, 30};
        stations[2] = new int[]{30, 30};
        stations[3] = new int[]{60, 40};
        System.out.println(solution.minRefuelStops(100, 10, stations));
    }
}